/**
 * 签到码相关的验证 Schema
 */

import { Type, Static } from '@sinclair/typebox'
import { UUIDSchema } from './common.schema'

// ==================== 生成签到码 ====================

/**
 * 生成签到码请求
 */
export const GenerateSecretCodeSchema = Type.Object(
    {
        eventId: UUIDSchema,
        generatedBy: UUIDSchema,
        expiresIn: Type.Optional(
            Type.Integer({
                minimum: 60,
                maximum: 3600,
                default: 300,
                description: '有效期（秒），默认300秒（5分钟）'
            })
        )
    },
    {
        $id: 'GenerateSecretCode',
        description: '生成签到码请求'
    }
)

export type GenerateSecretCodeRequest = Static<typeof GenerateSecretCodeSchema>

/**
 * 签到码响应
 */
export const SecretCodeSchema = Type.Object(
    {
        code: Type.String({
            pattern: '^\\d{6}$',
            description: '6位数字签到码'
        }),
        eventId: UUIDSchema,
        expiresAt: Type.String({
            format: 'date-time',
            description: '过期时间'
        }),
        generatedBy: UUIDSchema
    },
    {
        $id: 'SecretCode',
        description: '签到码信息'
    }
)

export const GenerateSecretCodeResponseSchema = Type.Object(
    {
        success: Type.Literal(true),
        data: SecretCodeSchema
    },
    {
        $id: 'GenerateSecretCodeResponse'
    }
)

// ==================== 验证签到码 ====================

/**
 * 验证签到码请求
 */
export const ValidateSecretCodeSchema = Type.Object(
    {
        eventId: UUIDSchema,
        code: Type.String({
            pattern: '^\\d{6}$',
            description: '6位数字签到码'
        })
    },
    {
        $id: 'ValidateSecretCode',
        description: '验证签到码请求'
    }
)

export type ValidateSecretCodeRequest = Static<typeof ValidateSecretCodeSchema>

export const ValidateSecretCodeResponseSchema = Type.Object(
    {
        success: Type.Literal(true),
        data: Type.Object({
            valid: Type.Boolean({ description: '签到码是否有效' }),
            message: Type.Optional(Type.String({ description: '验证消息' }))
        })
    },
    {
        $id: 'ValidateSecretCodeResponse'
    }
)

// ==================== 获取当前有效签到码 ====================

export const GetActiveSecretCodeResponseSchema = Type.Object(
    {
        success: Type.Literal(true),
        data: Type.Union([SecretCodeSchema, Type.Null()], {
            description: '当前有效的签到码，如果没有则为null'
        })
    },
    {
        $id: 'GetActiveSecretCodeResponse'
    }
)

// ==================== 撤销签到码 ====================

export const RevokeSecretCodeResponseSchema = Type.Object(
    {
        success: Type.Literal(true),
        data: Type.Object({
            revoked: Type.Boolean({ description: '是否成功撤销' }),
            count: Type.Optional(Type.Integer({ description: '撤销的签到码数量' }))
        })
    },
    {
        $id: 'RevokeSecretCodeResponse'
    }
)
